//
//  ItemGroupSettingViewController.m
//  MoneyBook
//
//  Created by Justin Kim on 11. 6. 27..
//  Copyright 2011 Daum. All rights reserved.
//

#import "ItemGroupSettingViewController.h"

@implementation ItemGroupSettingViewController
@synthesize itemGroupId = _itemGroupId;
/////////////////////////////////////////////////////////////////////////////////////////////////
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }   
    return self;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)dealloc{
    [_itemGroupId release];
    [super dealloc];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewDidLoad{
    self.title = @"분류 관리";    
    [super viewDidLoad];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewDidUnload{
    self.itemGroupId = nil;
    [super viewDidUnload];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.dataSourceArray = [[SQLHandler sharedInstance] findAllItemGroup];    
    [self.tableView reloadData];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
	[super shouldAutorotateToInterfaceOrientation:interfaceOrientation];
    return YES;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    
}
#pragma mark -
#pragma mark UIBarButtonItemDelegate
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)addAction{
    ItemGroupFormViewController* viewController = [[[ItemGroupFormViewController alloc] initWithNibName:@"ItemGroupFormView" bundle:nil] autorelease];
    [[self navigationController] pushViewController:viewController animated:YES];        
    [viewController createMode];
}
#pragma mark UIAlertViewDelegate mothods
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    SQLHandler *sql = [SQLHandler sharedInstance];
    NSArray *arr = [sql findAllItemByItemGroupById:_itemGroupId];
    for(NSDictionary *item in arr){
        NSLog(@"delete item:%d", [[item objectForKey:@"item_id"] intValue]);
        [sql deleteItemById:[item objectForKey:@"item_id"]];
    }
    NSLog(@"delete item group:%d", [_itemGroupId intValue]);    
    [sql deleteItemGroupById:_itemGroupId];
    NSLog(@"delete done");
    self.dataSourceArray = [[SQLHandler sharedInstance] findAllItemGroup];    
    [self.tableView reloadData];
}
#pragma mark -
#pragma mark UITableViewDelegate
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(_isEditMode){
        ItemGroupFormViewController* viewController = [[[ItemGroupFormViewController alloc] initWithNibName:@"ItemGroupFormView" bundle:nil] autorelease];
        [[self navigationController] pushViewController:viewController animated:YES];        
        [viewController loadItemGroup:[[_dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"item_group_id"]];

    }else{
        ItemSettingViewController* viewController = [[[ItemSettingViewController alloc] initWithNibName:@"ItemSettingView" bundle:nil] autorelease];
        [[self navigationController] pushViewController:viewController animated:YES];        
        [viewController loadItemByItemGroupId:[[_dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"item_group_id"]];
    }
}
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if([_dataSourceArray count]==1){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"경고" message:@"최소 한개는 있어야 합니다." delegate:nil cancelButtonTitle:@"확인" otherButtonTitles:nil];
        [alert show];
        [alert release];
        [self showDoneMode];
    }else{
        self.itemGroupId = [[_dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"item_group_id"];

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"주의" 
                                                        message:@"하위 분류가 모두 삭제 됩니다." 
                                                       delegate:self 
                                              cancelButtonTitle:@"취소" 
                                              otherButtonTitles:@"확인", nil];
        [alert show];
        [alert release];
    }
}
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath{
    [self moveRowAtIndexPath:fromIndexPath 
                 toIndexPath:toIndexPath
                   tableName:@"item_group"];
}
@end
